package top.infopub.mgr.bpa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.bpa.PrimarySureDto;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.OtherSureAttDao;
import top.infopub.mgr.bpa.dao.OtherSureDao;
import top.infopub.mgr.bpa.dao.OtherSureDetailDao;
import top.infopub.mgr.bpa.domain.OtherSureDomain;
import top.infopub.mgr.bpa.service.OtherSureService;
import top.infopub.mgr.bpa.vo.OtherSureVo;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.util.ReflectBeanUtils;
import top.infopub.model.bpa.OtherSure;
import top.infopub.model.bpa.OtherSureAtt;
import top.infopub.model.bpa.OtherSureDetail;

import java.util.Date;
import java.util.List;

@Service
public class OtherSureServiceImpl extends GenericServiceImpl<OtherSure, String> implements
		OtherSureService {

	@Autowired
	private OtherSureDao otherSureDao;
	
	@Autowired
    private WsFlowProcessService wsFlowProcess;

    @Autowired
    private OtherSureDetailDao otherSureDetailDao;
    
    @Autowired
    private OtherSureAttDao otherSureAttDao;

	@Override
	public GenericDao<OtherSure, String> getDao() {
		return otherSureDao;
	}

    @Override
    public Page<OtherSureDomain> selectPageByDomainWhere(Page<OtherSureDomain> page,
                                                         OtherSureVo otherSureVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<OtherSureDomain>)otherSureDao.selectByDomainWhere(otherSureVo);
    }

    @Override
    public Page<OtherSureDomain> selectMyCheckByDomainWhere(Page<OtherSureDomain> page,
                                                            OtherSureVo otherSureVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<OtherSureDomain>)otherSureDao.selectMyCheckByDomainWhere(otherSureVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(OtherSureVo otherSure, List<OtherSureDetail> otherSureDetails,
                     List<OtherSureAtt> otherSureAtts) {
        //插入记录
        otherSureDao.insertById(otherSure);

        //批量插入子项
        if(CollectionUtils.isNotEmpty(otherSureDetails)){
            otherSureDetailDao.insertByBatch(otherSureDetails);
        }
        
        //批量插入附件
        if(CollectionUtils.isNotEmpty(otherSureAtts)){
            otherSureAttDao.insertByBatch(otherSureAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.BPA_OTHER_SURE;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, otherSure.getId(), otherSure.getProcessId(), null);
        
    }

    @Override
    public OtherSureVo queryById(String id) {
        return otherSureDao.queryById(id);
    }

    @Override
    public String insertById(PrimarySureDto primarySureDto, String creator) {
        OtherSureVo sureVo = ReflectBeanUtils.do2bo(primarySureDto, OtherSureVo.class);
        Date date = new Date();
        sureVo.setAddBy(creator);
        sureVo.setAddTime(date);
        sureVo.setUptBy(creator);
        sureVo.setUptTime(date);
        otherSureDao.insertSelective(sureVo);
        return sureVo.getId();
    }

    @Override
    public void updateByIdSelective(PrimarySureDto primarySureDto, String modifier) {
        OtherSureVo sureVo = ReflectBeanUtils.do2bo(primarySureDto, OtherSureVo.class);
        sureVo.setId(primarySureDto.getId());
        Date date = new Date();
        sureVo.setUptBy(modifier);
        sureVo.setUptTime(date);
        otherSureDao.updateByIdSelective(sureVo);
    }

}